package data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import util.DateUtil;


public class SaveHistoryData {
	HistoryData hd = new HistoryData();
	public  void saveHistoryData() throws IOException{
		
		saveData("sh60");
		saveData("sz00");
		saveData("sz30");
		Set<String> codeSet = StockURLDataBuffer.getData().keySet();
		for (Iterator<String> iterator = codeSet.iterator(); iterator.hasNext();) {
			String code = (String) iterator.next();
			TimeStock ts = StockDataFetch.getTimeStock(code);
			if(ts!=null){
				hd.put(code, ts);
			}
		}
		System.out.println("股票数量"+hd.size());
		String fileName = DateUtil.format(DateUtil.getThisDate());
		FileOutputStream fos = new FileOutputStream("historydata"+File.separator+fileName+".sto");   
		ObjectOutputStream oos = new ObjectOutputStream(fos);      
		oos.writeObject(hd);    
		oos.flush();     
		oos.close();   
		
		FileOutputStream fos2 = new FileOutputStream("historydata"+File.separator+"last.sto"); 
		ObjectOutputStream oos2 = new ObjectOutputStream(fos2);  
		oos2.writeObject(hd);   
		oos2.flush();     
		oos2.close(); 
	}
	
	public void saveData(String city) throws IOException{
		int lenth=4000;
		Set<String> codeSet = new HashSet<String>();
		for(int i = 0;i<lenth;i++){
			String code = city;
			if(i<10){
				code=code+"000"+i;
			}else if(i>9&&i<100){
				code=code+"00"+i;
			}
			else if(i>99&&i<1000){
				code=code+"0"+i;
			}else if(i>999&&i<4000){
				code=code+i;
			}
			codeSet.add(code);
		}
		codeSet.add("sh000001");
		codeSet.add("sz399001");
		StockDataFetch.refreshData(codeSet);
	}
}
